<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input
            style="width: 500px"
            placeholder="请输入名称"
            maxlength="20"
            v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="记录内容" prop="text">
        <Tinymce v-model="form.text"></Tinymce>
      </el-form-item>
      <el-form-item label="联系电话:" prop="phone">
        <el-input
            style="width: 500px"
            placeholder="请输入电话"
            maxlength="11"
            v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="来电时间" prop="visitTime">
        <el-date-picker
            v-model="form.visitTime"
            type="datetime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary" @click="handlerSubmit" v-has-permi="['admin:mobile:save']">保存</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {detail, save} from "@/api/mobile";
import {getForm} from "@/utils/utils";
import UploadFile from "@/components/File/UploadFile.vue";
import Tinymce from "@/components/Tinymce/index.vue";

export default {
  components: {Tinymce, UploadFile},
  props:{
    id:{
      default:null
    }
  },
  data(){
    return{
      form:{
        id:null,
        name:'',
        phone:'',
        text:'',
        visitTime:'',
      },
      rules:{
        name: [{ required: true, message: '请输入名称', trigger: ['blur'] }],
        phone: [{ required: true, message: '请输入联系方式', trigger: ['blur'] }],
        text: [{ required: true, message: '请输入联系方式', trigger: ['blur'] }],
        visitTime: [{ required: true, message: '请选择时间', trigger: ['blur'] }],
      },
      roles:[],
      saveLoading:false,
      formLoading:false,
      uploads:{
        show:false,
        multiple:true,
      }
    }
  },
  mounted() {
    if(this.id){
      this.getDetail();
    }
  },
  methods:{
    getFile(fileList){
      // console.log(fileList);
    },
    getDetail(){
      this.formLoading=true
      detail(this.id).then(res=>{
        getForm(res,this.form);
      }).finally(()=>{
        this.formLoading=false;
      })
    },
    handlerSubmit(){
      if(this.saveLoading||this.formLoading){
        return;
      }
      this.$refs.form.validate(valid=>{
        if(!valid)return;
        this.saveLoading=true;
        save(this.form).then(()=>{
          this.$message.success('保存成功');
          this.close();
        }).finally(()=>{
          this.saveLoading=false
        })
      })
    },
    close(){
      this.$emit('hideEditDialog')
    }
  }
}
</script>
